package com.lwl.Algorithmic_data_structure.class03数据结构;

import com.lwl.Utils.LLinkNode;

import java.util.Random;

/**
 * 给一个链表，删除值为num的节点，返回当前列表的头节点
 */
public class Code03DeleteNumNode {

    public static void main(String[] args) {
        int num = new Random().nextInt(10);
        System.out.println(num);
        for (int i = 0; i < 10; i++) {
            LLinkNode head = LLinkNode.createRandomLink();
            head.printLinkList();
            LLinkNode res = deleteNumNode(head, num);
            res.printLinkList();
        }
    }

    public static LLinkNode deleteNumNode(LLinkNode head, int num) {
        // 使用一个空节点作为链表的头
        LLinkNode dummyNode = new LLinkNode(-1);
        dummyNode.next = head;
        head = dummyNode;
        LLinkNode pre = head;

        while (head != null){
            if (head.value == num){
                pre.next = head.next;
            }else{
                pre = head;
            }
            head = head.next;
        }
        return dummyNode.next;
    }
}
